package com.rzico.sbl.ui.report.dispatch

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.rzico.sbl.databinding.FragmentDispatchThirdBinding
import com.rzico.sbl.other.serviceStep
import com.rzico.sbl.viewmodel.ReportDispatchViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.doubleValue
import com.xinnuo.common_ui.base.NavigationFragment
import com.xinnuo.common_ui.base.refresh
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeBySuccess
import org.json.JSONObject

class DispatchThirdFragment
@JvmOverloads constructor(
    private val shippingSn: String = ""
) : NavigationFragment() {

    private var mBinding: FragmentDispatchThirdBinding? = null

    override fun getViewModel() = get<ReportDispatchViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentDispatchThirdBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        mBinding?.let {
            it.thirdScroll.setOnTouchListener { _, _ ->
                return@setOnTouchListener it.thirdSwipe.isRefreshing
            }

            it.thirdSwipe.apply {
                refresh { getData() }
                isRefreshing = true
            }

            getData()
        }
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .dispatchPay(shippingSn, false)
            .bindLifeCycle(getLifecycleOwner())
            .subscribeBySuccess(
                event = getViewModel(),
                onSuccess = { result ->
                    val obj = JSONObject(result)

                    mBinding?.let {
                        val amountBucket = obj.optNotNull("barrelPrice").doubleValue
                        val amountRotten = obj.optNotNull("swapPrice").doubleValue
                        val amountPay =
                            when (serviceStep) {
                                "2" -> {
                                    it.thirdTicketLl.visible()
                                    obj.optNotNull("amountPayable").doubleValue
                                }
                                else -> obj.optNotNull("subPrice").doubleValue
                            }

                        it.thirdGoods.text = amountPay.formatDecimal()
                        it.thirdBucket.text = amountBucket.formatDecimal()
                        it.thirdRotten.text = amountRotten.formatDecimal()
                        it.thirdTotal.text =
                            (amountBucket + amountPay - amountRotten).formatDecimal()
                        it.thirdFee.text =
                            obj.optNotNull("freightPayable")
                                .apply {
                                    it.thirdFeeLl.isVisible = doubleValue > 0
                                }
                                .formatDecimal()
                        it.thirdTicket.text = obj.optNotNull("exchangePrice").formatDecimal()
                        it.thirdNew.text = obj.optNotNull("debt").formatDecimal()
                        it.thirdBack.text = obj.optNotNull("repayment").formatDecimal()

                        it.thirdOut.text = obj.optNotNull("payAmount").formatDecimal()
                        it.thirdReal.text = obj.optNotNull("amountPaid").formatDecimal()
                        it.thirdWechat.text = obj.optNotNull("weixinPay").formatDecimal()
                        it.thirdAlipay.text = obj.optNotNull("aliPay").formatDecimal()
                        it.thirdCash.text = obj.optNotNull("cashPay").formatDecimal()
                        it.thirdMemo.text = obj.optNotNull("memo").trimEnd('\n')
                    }
                },
                onFinally = {
                    mBinding?.let {
                        it.thirdSwipe.isRefreshing = false
                    }
                }
            )
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

    companion object {
        fun newInstance(shippingSn: String) = DispatchThirdFragment(shippingSn)
    }

}